package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.CourseArrangeRequestVO;
import com.yugong.entity.vo.response.CourseArrangeListResponseVO;
import com.yugong.mapper.*;
import com.yugong.service.CourseArrangeService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 课程安排业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class CourseArrangeServiceImpl extends ServiceImpl<CourseArrangeMapper, CourseArrange> implements CourseArrangeService {
    @Resource
    private CourseArrangeMapper courseArrangeMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private CourseScoreMapper courseScoreMapper;
    @Resource
    private OrderClassMapper orderClassMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 新增课程安排
     *
     * @param courseArrangeRequestVO 课程安排请求实体类
     * @return 响应码
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addCourseArrange(CourseArrangeRequestVO courseArrangeRequestVO) {
        Date createTime = new Date();
        CourseArrange courseArrange = new CourseArrange();
        courseArrange.setCourseArrangeCourseNo(courseArrangeRequestVO.getCourseArrangeCourseNo());
        courseArrange.setCourseArrangeTeacherNo(courseArrangeRequestVO.getCourseArrangeTeacherNo());
        courseArrange.setCourseArrangeOrderClassNo(courseArrangeRequestVO.getCourseArrangeOrderClassNo());
        courseArrange.setCourseArrangeWeekNumber(courseArrangeRequestVO.getCourseArrangeWeekNumber());
        courseArrange.setCourseArrangeStartTime(courseArrangeRequestVO.getCourseArrangeStartTime());
        courseArrange.setCourseArrangeEndTime(courseArrangeRequestVO.getCourseArrangeEndTime());
        courseArrange.setCourseArrangeRoomNo(courseArrangeRequestVO.getCourseArrangeRoomNo());
        courseArrange.setCourseArrangeClassHour(courseArrangeRequestVO.getCourseArrangeClassHour());
        courseArrange.setCourseArrangeCredit(courseArrangeRequestVO.getCourseArrangeCredit());
        courseArrange.setCourseArrangeSemester(courseArrangeRequestVO.getCourseArrangeSemester());
        courseArrange.setCourseArrangeWeeks(courseArrangeRequestVO.getCourseArrangeWeeks());
        courseArrange.setCourseArrangeCreateMan(courseArrangeRequestVO.getCourseArrangeCreateMan());
        courseArrange.setCourseArrangeCreateTime(createTime);
        OrderClass orderClass = orderClassMapper.selectById(courseArrangeRequestVO.getCourseArrangeOrderClassNo());
        if (orderClass != null) {
            if (0 == orderClass.getOrderClassState() ||
                    1 == orderClass.getOrderClassState()) {
                return courseArrangeMapper.insert(courseArrange);
            } else if (2 == orderClass.getOrderClassState()) {
                int addCourseArrangeFlag = courseArrangeMapper.insert(courseArrange);
                int addCourseScoreFlag = 0;
                int addFlag = 0;
                LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                studentLambdaQueryWrapper.eq(Student::getStudentOrderClassNo, courseArrangeRequestVO.getCourseArrangeOrderClassNo());
                List<Student> studentList = studentMapper.selectList(studentLambdaQueryWrapper);
                for (Student student : studentList) {
                    CourseScore courseScore = new CourseScore();
                    courseScore.setCourseScoreOrderClassNo(courseArrangeRequestVO.getCourseArrangeOrderClassNo());
                    courseScore.setCourseScoreCourseNo(courseArrangeRequestVO.getCourseArrangeCourseNo());
                    courseScore.setCourseScoreStudentNo(student.getStudentNo());
                    courseScore.setCourseScoreCreateMan(courseArrangeRequestVO.getCourseArrangeCreateMan());
                    courseScore.setCourseScoreCreateTime(createTime);
                    addCourseScoreFlag = courseScoreMapper.insert(courseScore);
                    addFlag = addFlag + addCourseScoreFlag;
                }
                return addCourseArrangeFlag + addFlag;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * 查询课程安排
     *
     * @param pageNum           当前页
     * @param pageSize          当前页数据个数
     * @param orderClassAdminNo 订单班管理员工号
     * @param orderClassName    班名
     * @return 课程安排
     */
    @Override
    public PageInfo<CourseArrangeListResponseVO> findCourseArrangePage(Integer pageNum, Integer pageSize, String orderClassAdminNo, String orderClassName) {
        PageHelper.startPage(pageNum, pageSize);
        // 查询原始的课程安排列表
        LambdaQueryWrapper<CourseArrange> courseArrangeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseArrangeLambdaQueryWrapper
                .eq(CourseArrange::getCourseArrangeCreateMan, orderClassAdminNo)
                .orderByDesc(CourseArrange::getCourseArrangeCreateTime);
        List<CourseArrange> courseArrangeList = courseArrangeMapper.selectList(courseArrangeLambdaQueryWrapper);
        QueryWrapper<CourseArrange> courseArrangeQueryWrapper = new QueryWrapper<>();
        courseArrangeQueryWrapper.eq("course_arrange_create_man", orderClassAdminNo);
        Long courseArrangeCountNumber = courseArrangeMapper.selectCount(courseArrangeQueryWrapper);
        List<CourseArrangeListResponseVO> courseArrangeListResponseVOList = new ArrayList<>();
        for (CourseArrange courseArrange : courseArrangeList) {
            CourseArrangeListResponseVO courseArrangeListResponseVO = new CourseArrangeListResponseVO();
            courseArrangeListResponseVO.setCourseArrangeNo(courseArrange.getCourseArrangeNo());
            if (courseArrange.getCourseArrangeOrderClassNo() == null) {
                courseArrangeListResponseVO.setCourseArrangeOrderClassNo("无");
                courseArrangeListResponseVO.setCourseArrangeOrderClassName("无");
            } else {
                LambdaQueryWrapper<OrderClass> orderClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
                orderClassLambdaQueryWrapper
                        .eq(OrderClass::getOrderClassNo, courseArrange.getCourseArrangeOrderClassNo())
                        .like(OrderClass::getOrderClassName, orderClassName);
                OrderClass orderClass = orderClassMapper.selectOne(orderClassLambdaQueryWrapper);
                if (orderClass == null) {
                    courseArrangeListResponseVO.setCourseArrangeOrderClassNo("无");
                    courseArrangeListResponseVO.setCourseArrangeOrderClassName("无");
                    courseArrangeListResponseVO.setCourseArrangeCourseNo("无");
                    courseArrangeListResponseVO.setCourseArrangeCourseName("无");
                    courseArrangeListResponseVO.setCourseArrangeTeacherNo("无");
                    courseArrangeListResponseVO.setCourseArrangeTeacherName("无");
                    continue;
                } else {
                    if (orderClass.getOrderClassNo() == null) {
                        courseArrangeListResponseVO.setCourseArrangeOrderClassNo("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeOrderClassNo(orderClass.getOrderClassNo());
                    }
                    if (orderClass.getOrderClassName() == null) {
                        courseArrangeListResponseVO.setCourseArrangeOrderClassName("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeOrderClassName(orderClass.getOrderClassName());
                    }
                }
            }
            if (courseArrange.getCourseArrangeCourseNo() == null) {
                courseArrangeListResponseVO.setCourseArrangeCourseNo("无");
                courseArrangeListResponseVO.setCourseArrangeCourseName("无");
            } else {
                Course course = courseMapper.selectById(courseArrange.getCourseArrangeCourseNo());
                if (course == null) {
                    courseArrangeListResponseVO.setCourseArrangeCourseNo("无");
                    courseArrangeListResponseVO.setCourseArrangeCourseName("无");
                } else {
                    if (course.getCourseNo() == null) {
                        courseArrangeListResponseVO.setCourseArrangeCourseNo("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeCourseNo(course.getCourseNo());
                    }
                    if (course.getCourseName() == null) {
                        courseArrangeListResponseVO.setCourseArrangeCourseName("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeCourseName(course.getCourseName());
                    }
                }
            }
            if (courseArrange.getCourseArrangeTeacherNo() == null) {
                courseArrangeListResponseVO.setCourseArrangeTeacherNo("无");
                courseArrangeListResponseVO.setCourseArrangeTeacherName("无");
            } else {
                Teacher teacher = teacherMapper.selectById(courseArrange.getCourseArrangeTeacherNo());
                if (teacher == null) {
                    courseArrangeListResponseVO.setCourseArrangeTeacherNo("无");
                    courseArrangeListResponseVO.setCourseArrangeTeacherName("无");
                } else {
                    if (teacher.getTeacherNo() == null) {
                        courseArrangeListResponseVO.setCourseArrangeTeacherNo("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeTeacherNo(teacher.getTeacherNo());
                    }
                    if (teacher.getTeacherName() == null) {
                        courseArrangeListResponseVO.setCourseArrangeTeacherName("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeTeacherName(teacher.getTeacherName());
                    }
                }
            }
            if (courseArrange.getCourseArrangeRoomNo() == null) {
                courseArrangeListResponseVO.setCourseArrangeRoomNo("无");
                courseArrangeListResponseVO.setCourseArrangeRoomName("无");
            } else {
                Room room = roomMapper.selectById(courseArrange.getCourseArrangeRoomNo());
                if (room == null) {
                    courseArrangeListResponseVO.setCourseArrangeRoomNo("无");
                    courseArrangeListResponseVO.setCourseArrangeRoomName("无");
                } else {
                    if (room.getRoomNo() == null) {
                        courseArrangeListResponseVO.setCourseArrangeRoomNo("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeRoomNo(room.getRoomNo());
                    }
                    if (room.getRoomName() == null) {
                        courseArrangeListResponseVO.setCourseArrangeRoomName("无");
                    } else {
                        courseArrangeListResponseVO.setCourseArrangeRoomName(room.getRoomName());
                    }
                }
            }
            if ("0".equals(courseArrange.getCourseArrangeWeekNumber())) {
                courseArrangeListResponseVO.setCourseArrangeWeekNumber("无");
            } else {
                courseArrangeListResponseVO.setCourseArrangeWeekNumber(courseArrange.getCourseArrangeWeekNumber());
            }
            if (courseArrange.getCourseArrangeStartTime() == null) {
                courseArrangeListResponseVO.setCourseArrangeStartTime(null);
            } else {
                courseArrangeListResponseVO.setCourseArrangeStartTime(courseArrange.getCourseArrangeStartTime());
            }
            if (courseArrange.getCourseArrangeEndTime() == null) {
                courseArrangeListResponseVO.setCourseArrangeEndTime(null);
            } else {
                courseArrangeListResponseVO.setCourseArrangeEndTime(courseArrange.getCourseArrangeEndTime());
            }
            if (courseArrange.getCourseArrangeClassHour() == null) {
                courseArrangeListResponseVO.setCourseArrangeClassHour(BigDecimal.valueOf(0));
            } else {
                courseArrangeListResponseVO.setCourseArrangeClassHour(courseArrange.getCourseArrangeClassHour());
            }
            if (courseArrange.getCourseArrangeCredit() == null) {
                courseArrangeListResponseVO.setCourseArrangeCredit(BigDecimal.valueOf(0));
            } else {
                courseArrangeListResponseVO.setCourseArrangeCredit(courseArrange.getCourseArrangeCredit());
            }
            if ("0".equals(courseArrange.getCourseArrangeSemester())) {
                courseArrangeListResponseVO.setCourseArrangeSemester("无");
            } else {
                courseArrangeListResponseVO.setCourseArrangeSemester(courseArrange.getCourseArrangeSemester());
            }
            if (courseArrange.getCourseArrangeWeeks() == null) {
                courseArrangeListResponseVO.setCourseArrangeWeeks("无");
            } else {
                courseArrangeListResponseVO.setCourseArrangeWeeks(courseArrange.getCourseArrangeWeeks());
            }
            if (courseArrange.getCourseArrangeCreateMan() == null) {
                courseArrangeListResponseVO.setCourseArrangeCreateMan("无");
            } else {
                courseArrangeListResponseVO.setCourseArrangeCreateMan(courseArrange.getCourseArrangeCreateMan());
            }
            if (courseArrange.getCourseArrangeCreateTime() == null) {
                courseArrangeListResponseVO.setCourseArrangeCreateTime(null);
            } else {
                String createTime = timeUtils.setDateToStrDate(courseArrange.getCourseArrangeCreateTime(), Const.YYYY_MM_DD);
                courseArrangeListResponseVO.setCourseArrangeCreateTime(createTime);
            }
            if (courseArrange.getCourseArrangeUpdateMan() == null) {
                courseArrangeListResponseVO.setCourseArrangeUpdateMan("无");
            } else {
                courseArrangeListResponseVO.setCourseArrangeUpdateMan(courseArrange.getCourseArrangeUpdateMan());
            }
            if (courseArrange.getCourseArrangeUpdateTime() == null) {
                courseArrangeListResponseVO.setCourseArrangeUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(courseArrange.getCourseArrangeUpdateTime(), Const.YYYY_MM_DD);
                courseArrangeListResponseVO.setCourseArrangeUpdateTime(updateTime);
            }
            courseArrangeListResponseVO.setCourseArrangeCountNumber(courseArrangeCountNumber);
            courseArrangeListResponseVOList.add(courseArrangeListResponseVO);
        }
        return PageInfo.of(courseArrangeListResponseVOList);
    }

    /**
     * 更新课程安排
     *
     * @param courseArrangeRequestVO 课程安排请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateCourseArrange(CourseArrangeRequestVO courseArrangeRequestVO) {
        LambdaUpdateWrapper<CourseArrange> courseArrangeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        courseArrangeLambdaUpdateWrapper
                .eq(CourseArrange::getCourseArrangeNo, courseArrangeRequestVO.getCourseArrangeNo())
                .set(CourseArrange::getCourseArrangeOrderClassNo, courseArrangeRequestVO.getCourseArrangeOrderClassNo())
                .set(CourseArrange::getCourseArrangeCourseNo, courseArrangeRequestVO.getCourseArrangeCourseNo())
                .set(CourseArrange::getCourseArrangeTeacherNo, courseArrangeRequestVO.getCourseArrangeTeacherNo())
                .set(CourseArrange::getCourseArrangeWeekNumber, courseArrangeRequestVO.getCourseArrangeWeekNumber())
                .set(CourseArrange::getCourseArrangeStartTime, courseArrangeRequestVO.getCourseArrangeStartTime())
                .set(CourseArrange::getCourseArrangeEndTime, courseArrangeRequestVO.getCourseArrangeEndTime())
                .set(CourseArrange::getCourseArrangeRoomNo, courseArrangeRequestVO.getCourseArrangeRoomNo())
                .set(CourseArrange::getCourseArrangeClassHour, courseArrangeRequestVO.getCourseArrangeClassHour())
                .set(CourseArrange::getCourseArrangeCredit, courseArrangeRequestVO.getCourseArrangeCredit())
                .set(CourseArrange::getCourseArrangeSemester, courseArrangeRequestVO.getCourseArrangeSemester())
                .set(CourseArrange::getCourseArrangeWeeks, courseArrangeRequestVO.getCourseArrangeWeeks())
                .set(CourseArrange::getCourseArrangeUpdateMan, courseArrangeRequestVO.getCourseArrangeUpdateMan())
                .set(CourseArrange::getCourseArrangeUpdateTime, new Date());
        return courseArrangeMapper.update(courseArrangeLambdaUpdateWrapper);
    }

    /**
     * 删除课程安排
     *
     * @param courseArrangeNo 主键
     * @return 数据库影响行数
     */
    @Override
    public int deleteCourseArrange(Integer courseArrangeNo) {
        return courseArrangeMapper.deleteById(courseArrangeNo);
    }
}
